<!--
 * @Author: wuzhen
 * @Date: 2019-12-04 17:56:54
 * @LastEditors  : wuzhen
 * @LastEditTime : 2020-01-09 14:29:29
 * @Description: 通知管理
 -->
<template>
  <Card style="width:100%" dis-hover>
    <!-- 搜索表头/表头按钮 -->
    <search-table :label-width="90" :search-props="searchProps" :search-buttons="searchButtons" @handle-filter="handleFilter" @handle-click="handleClick" />
    <wz-table ref="table" :search-api="searchApi" :columns="columns" @handle-click="handleClick" />
    <!-- 添加、编辑 -->
    <Modal v-model="editDrawer" :title="editTitle" @on-cancel="editDrawer = false" width="500">
      <wz-form v-if="editDrawer" ref="form" :label-width="100" :form-obj="formObj" :form-props="formProps" />
      <div slot="footer">
        <Button type="text" @click="editDrawer = false">取消</Button>
        <Button type="primary" @click="onSubmit">确定</Button>
      </div>
    </Modal>
  </Card>
</template>
<script>
import index from "./mixins";
import form from "./mixins/form";
import { getNoticeList, addNotice } from "@/request/api";
export default {
  mixins: [index, form],
  data() {
    return {
      searchApi: getNoticeList,
      apiParams: {
        flipper: {
          offset: 0,
          limit: 15,
          sort: "createtime desc"
        }
      },
      editDrawer: false,
      editTitle: "添加",
      formObj: {},
      type: "手机号",
      keyword: ""
    };
  },
  mounted() {
    this.getData();
  },
  methods: {
    // 获取table数据
    getData() {
      this.$refs.table.getData(this.apiParams);
    },
    // 搜索
    handleFilter(params) {
      let apiParams = {};
      if (params.date && params.date.length && params.date[0]) {
        apiParams["createtime"] = {
          min: new Date(params.date[0] + " 00:00:00").getTime(),
          max: new Date(params.date[1] + " 23:59:59").getTime()
        };
      }
      apiParams = { ...apiParams, ...params };
      this.apiParams["bean"] = { ...apiParams };
      this.apiParams["flipper"] = {
        offset: 0,
        limit: 15,
        sort: "createtime desc"
      };
      this.getData();
    },
    handleClick(type, row) {
      this.formObj = row || {};
      this.type = type;
      this.roleDrawer = false;
      switch (type) {
        case "add":
          this.editTitle = "添加";
          break;
        default:
          break;
      }
      this.editDrawer = true;
    },
    changeReportSize() {},
    changeReportPage() {},
    // 确定
    async onSubmit() {
      try {
        const validate = await this.$refs.form.validate();
        if (validate.valid) {
          let res;
          if (this.type === "add") {
            res = await addNotice({
              bean: JSON.stringify({
                ...validate.formData
              })
            });
          }
          if (res.code === 200) {
            this.$Message.success("操作成功");
            this.editDrawer = false;
            this.getData();
          } else {
            this.$Message.error("操作失败");
          }
        }
      } catch (error) {
        throw error;
      }
    }
  }
};
</script>
